package com.peas.platform.module.icims.expenditure.kpis.domain.impl;

import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.enums.CommonYesNo;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.expenditure.kpis.adapter.repository.ExpenditureAdjustmentRepository;
import com.peas.platform.module.icims.expenditure.kpis.adapter.repository.po.ExpenditureAdjustmentPo;
import com.peas.platform.module.icims.expenditure.kpis.domain.ExpenditureAdjustmentDomain;
import com.peas.platform.module.icims.expenditure.kpis.dto.ExpenditureAdjustmentDto;
import com.peas.platform.module.icims.expenditure.kpis.dto.ExpenditureAdjustmentItemDto;
import com.peas.platform.module.icims.expenditure.kpis.dto.ExpenditureKpiDto;
import com.peas.platform.module.icims.expenditure.kpis.service.ExpenditureAdjustmentItemService;
import com.peas.platform.module.icims.expenditure.kpis.service.ExpenditureKpiService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Component
public class ExpenditureAdjustmentDomainImpl extends BaseDomainImpl<ExpenditureAdjustmentDto, ExpenditureAdjustmentPo> implements ExpenditureAdjustmentDomain {

    @Resource
    private ExpenditureAdjustmentRepository expenditureAdjustmentRepository;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private ExpenditureAdjustmentItemService expenditureAdjustmentItemService;
    @Resource
    private ExpenditureKpiService expenditureKpiService;

    @Override
    public BaseRepository<ExpenditureAdjustmentPo> getMapper() {
        return expenditureAdjustmentRepository;
    }

    @Override
    public Class<? extends BaseRepository<ExpenditureAdjustmentPo>> getRepositoryClass() {
        return ExpenditureAdjustmentRepository.class;
    }

    @Transactional
    @Override
    public int save(ExpenditureAdjustmentDto dto) {
        if (StrUtil.isBlank(dto.getAdjustmentCode())) {
            String code = sequenceService.genSequence("expenditure", "ExpenditureAdjustmentDto", "EADD", 4, 1);
            dto.setAdjustmentCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("adjustmentCode", dto.getAdjustmentCode());
        //回写指标单
        ExpenditureKpiDto kpi = expenditureKpiService.getById(dto.getExpenditureKpiId());
        if (!StrUtil.equals(CommonYesNo.yes.getKey(), kpi.getIsBegin())) {
            kpi.setIsBegin(CommonYesNo.yes.getKey());
            expenditureKpiService.updateById(kpi);
        }
        return result;
    }

    @Transactional
    @Override
    public int updateById(ExpenditureAdjustmentDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("adjustmentCode", dto.getAdjustmentCode());
        //回写指标单
        ExpenditureKpiDto kpi = expenditureKpiService.getById(dto.getExpenditureKpiId());
        if (!StrUtil.equals(CommonYesNo.yes.getKey(), kpi.getIsBegin())) {
            kpi.setIsBegin(CommonYesNo.yes.getKey());
            expenditureKpiService.updateById(kpi);
        }
        return result;
    }

    @Transactional
    @Override
    public int removeById(Serializable id) {
        int result = super.removeById(id);
        List<ExpenditureAdjustmentItemDto> list = expenditureAdjustmentItemService.listByField("expenditureAdjustmentId", id);
        expenditureAdjustmentItemService.removeBatchByList(list);
        return result;
    }

}
